package cn.tedu.zhyl.appointment.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.tedu.zhyl.appointment.pojo.dto.ReservationDTO;
import cn.tedu.zhyl.appointment.pojo.dto.ReservationParam;
import cn.tedu.zhyl.appointment.pojo.vo.ReservationVO;
import cn.tedu.zhyl.appointment.pojo.vo.TimeCountVO;
import cn.tedu.zhyl.appointment.service.ReservationService;
import cn.tedu.zhyl.base.response.JsonResult;
import cn.tedu.zhyl.base.response.PageResponse;
import cn.tedu.zhyl.base.response.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "预约管理")
public class ReservationController {


    @Autowired
    private ReservationService reservationService;

    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    public JsonResult<PageResponse<ReservationVO>> findByPage(
            @RequestParam(defaultValue = "1")Integer pageNum,
            @RequestParam(defaultValue = "10")Integer pageSize,
            Integer status
    ){
        PageResponse<ReservationVO> list = reservationService.findByPage(pageNum,pageSize,status);
        return JsonResult.ok(list);
    }

    @GetMapping("/cancelled-count")
    @ApiOperation("查询用户当天的取消预约的次数")
    public JsonResult<Integer> getCancelledCount(){
        Integer cancelledCount = reservationService.getCancelledCount();
        return JsonResult.ok(cancelledCount);
    }

    @GetMapping("/countByTime")
    @ApiOperation("查询用户每个时间段的预约次数")
    public JsonResult<List<TimeCountVO>> getCountByTime(@RequestParam("time")Long time){
        List<TimeCountVO> list = reservationService.getCountByTime(LocalDateTimeUtil.of(time));
        return JsonResult.ok(list);
    }


    @PostMapping
    @ApiOperation("添加预约")
    public JsonResult addReservation(@RequestBody ReservationDTO dto){
        reservationService.addReservation(dto);
        return JsonResult.ok();
    }


    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public JsonResult cancelReservation(@PathVariable Long id){
        reservationService.cancelReservation(id);
        return JsonResult.ok();
    }


}
